/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: 西安华信智慧数字科技有限公司
 * 系统版本: 1.0.0
 * 开发人员: zhangfb
 * 开发时间: 2020/3/1 23:59
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.hyacinth.cloudnote.common.security;


import com.hyacinth.cloudnote.common.constants.DatePattern;
import com.hyacinth.cloudnote.common.enums.UserStatus;
import com.hyacinth.cloudnote.common.exception.AppRuntimeException;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.StringJoiner;
import java.util.UUID;

/**
 * UserPrincipal 解析器
 *
 * @author zhangfb
 * @version 1.0.0.1
 * @since 2020/3/1 23:59
 */
@UtilityClass
public final class UserPrincipalParser {

    private static final String TOKEN_USER_PROP_DELIMITER = "$";

    public static String pack(UserPrincipal principal) {
        StringJoiner joiner = new StringJoiner(TOKEN_USER_PROP_DELIMITER);
        joiner.add(String.valueOf(principal.getId()))
                .add(principal.getUsername())
                .add(principal.getMobile())
                .add(principal.getEmail())
                .add(principal.getRealName())
                .add(String.valueOf(principal.getStatus()))
                .add(principal.getPortrait())
                .add(principal.getCreateTime().format(DateTimeFormatter.ofPattern(DatePattern.PURE_DATETIME_PATTERN)));
        return joiner.toString();
    }


    public static UserPrincipal parse(String token) {
        String[] extData = StringUtils.split(token, TOKEN_USER_PROP_DELIMITER);
        if (extData.length < 8) {
            throw new AppRuntimeException("Token载荷数据格式不合法");
        }
        final UUID id = UUID.fromString(extData[0]);
        final String username = parseJwtProp(extData[1]);
        final String mobile = parseJwtProp(extData[2]);
        final String email = parseJwtProp(extData[3]);
        final String realName = parseJwtProp(extData[4]);
        final String status = parseJwtProp(extData[5]);
        final String portrait = parseJwtProp(extData[6]);
        final String createTime = parseJwtProp(extData[7]);

        UserPrincipal userPrincipal = new UserPrincipal(id, username, mobile,
                email, realName, UserStatus.valueOf(status), portrait);
        if (StringUtils.isNotBlank(createTime)) {
            LocalDateTime time = LocalDateTime.parse(createTime, DateTimeFormatter.ofPattern(DatePattern.PURE_DATETIME_PATTERN));
            userPrincipal.setCreateTime(time);
        }

        return userPrincipal;
    }

    private static String parseJwtProp(String prop) {
        if ("null".equalsIgnoreCase(prop)) {
            return null;
        }
        return prop;
    }


//    public static void main(String[] args) throws InterruptedException {
//        String pack = pack(new UserPrincipal(UUID.randomUUID(), null, "15502916770", null, null, UserStatus.ACTIVE, null));
//        System.out.println(pack);
//
//        Thread.sleep(10000);
//
//        UserPrincipal parse = parse(pack);
//        System.out.println(parse);
//    }
}
